package com.ljx.video.mq.producer;

import com.ljx.video.mq.dto.MqTransferDto;
import com.ljx.video.mq.enums.MQTypeEnum;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-11-05  22:28
 */
@Component
public class RabbitMQProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMQMsg(MqTransferDto mqTransferDto) {
        rabbitTemplate.convertAndSend(getExchange(mqTransferDto.getType()), mqTransferDto.getRoutingKey(), mqTransferDto.getObj());
    }

    private String getExchange(Integer type) {
        for (MQTypeEnum item : MQTypeEnum.values()) {
            if (item.getKey().equals(type)){
                return item.getExchange();
            }
        }
        return MQTypeEnum.DEFAULT.getExchange();
    }


}
